package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

//CORSMiddeware 解决同源策略问题
func CORSMiddeware(c *gin.Context) {
	c.Writer.Header().Set("Access-Control-Allow-Origin", "*")  //设置允许访问的域名
	c.Writer.Header().Set("Access-Control-Max-Age", "86400")   //设置最大缓存时间
	c.Writer.Header().Set("Access-Control-Allow-Methods", "*") //设置能访问的方法
	c.Writer.Header().Set("Access-Control-Allow-Headers", "*") //设置能携带的headers
	c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
	if c.Request.Method == http.MethodOptions {
		c.AbortWithStatus(200)
	} else {
		c.Next()
	}
}
